package com.wonders.hs.pd.mm.web.utils;

import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

import static com.wonders.hs.pd.mm.web.utils.ExcelFileGenerator.exportExcelByHttpServletResponse;

/**
 * 医保代码导出
 *
 * @author banglong
 * @date 2017/5/18
 */
public class MedicareCodeExcelUtil {

  public static void exportExcelFile(Map<String, Object> page_div, HttpServletResponse response) {

    //初始化fieldName，fieldDate
    ArrayList<String> fieldName = Lists.newArrayList(((String) page_div.get("fieldName")).split(","));    //excel标题数据集
    ArrayList<Map<String, String>> fieldData = (ArrayList<Map<String, String>>) page_div.get("data");    //excel数据内容

    HSSFWorkbook workbook = buildExcelFile(fieldName, fieldData);

    // 导出 excel
    exportExcelByHttpServletResponse(page_div, response, outputStream -> {
      try {
        workbook.write(outputStream);
        outputStream.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    });
  }

  private static HSSFWorkbook buildExcelFile(ArrayList<String> fieldName, ArrayList<Map<String, String>> fieldData) {
    HSSFWorkbook workbook = new HSSFWorkbook();

    int rows = fieldData.size();
    int sheetNum = 0;

    int SPLIT_COUNT = fieldData.size() + 1;
    if (rows % SPLIT_COUNT == 0) {
      sheetNum = rows / SPLIT_COUNT;
    } else {
      sheetNum = rows / SPLIT_COUNT + 1;
    }

    for (int i = 1; i <= sheetNum; i++) {

      HSSFSheet sheet = workbook.createSheet("页面 " + i);

      for (int j = 0; j < 10; j++) {
        //设置所有单元格的宽度
        sheet.setColumnWidth(j, 3000);
      }

      HSSFCellStyle contentStyle = workbook.createCellStyle(); // 内容样式
      contentStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
      contentStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
      contentStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
      contentStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框

      int index = 0;

      // 设置内容
      for (int k = 0; k < (rows < SPLIT_COUNT ? rows : SPLIT_COUNT); k++) {

        //将数据内容放入excel单元格
        Map<String, String> rowList = fieldData.get(k);

        // 判断是否是需要设置表头
        if ("title".equals(rowList.get("title"))) {
          // 空一行
          if (k != 0) {
            HSSFRow row = sheet.createRow(index++);
            HSSFCell cell = row.createCell(0);
            cell.setCellValue("");
          }

          HSSFRow row = sheet.createRow(index++);
          // 设置供应商名称
          HSSFCell cell1 = row.createCell(0);
          cell1.setCellStyle(contentStyle);
          cell1.setCellValue("供应商名称：");

          HSSFCell cell2 = row.createCell(1);
          cell2.setCellStyle(contentStyle);
          cell2.setCellValue(rowList.get("供应商名称"));

          HSSFCell cell3 = row.createCell(2);
          cell3.setCellStyle(contentStyle);
          cell3.setCellValue("");

          // 设置供应商联系人
          HSSFCell cell4 = row.createCell(3);
          cell4.setCellStyle(contentStyle);
          cell4.setCellValue("供应商联系人：");

          HSSFCell cell5 = row.createCell(4);
          cell5.setCellStyle(contentStyle);
          cell5.setCellValue(rowList.get("供应商联系人"));

          HSSFCell cell6 = row.createCell(5);
          cell6.setCellStyle(contentStyle);
          cell6.setCellValue("");

          // 设置供应商联系电话
          HSSFCell cell7 = row.createCell(6);
          cell7.setCellStyle(contentStyle);
          cell7.setCellValue("供应商联系电话：");

          HSSFCell cell8 = row.createCell(7);
          cell8.setCellStyle(contentStyle);
          cell8.setCellValue(rowList.get("供应商联系电话"));

          for (int j = 8; j < 10; j++) {
            HSSFCell cell = row.createCell(j);
            cell.setCellStyle(contentStyle);
            cell.setCellValue("");
          }

          HSSFRow headRow = sheet.createRow(index++);
          // 设置表头
          setTableHead(fieldName, workbook, headRow);

        } else {

          HSSFRow row = sheet.createRow(index++);

          for (int j = 0; j < fieldName.size(); j++) {
            String aFieldName = fieldName.get(j);
            HSSFCell cell = row.createCell(j);

            String result = rowList.get(aFieldName) == null ? "" : rowList.get(aFieldName);
            cell.setCellStyle(contentStyle);
            if (!StringUtils.isEmpty(result)) {
              cell.setCellValue(result);
            } else {
              cell.setCellValue("");
            }
          }
        }
      }
    }
    return workbook;
  }

  private static void setTableHead(ArrayList fieldName, HSSFWorkbook workbook, HSSFRow headRow) {
    // 设置标题
    for (int j = 0; j < fieldName.size(); j++) {
      HSSFCell cell = headRow.createCell(j);
      //添加样式
      //cell.setCellType(HSSFCell.CELL_TYPE_STRING);
      //设置所有单元格的宽度
//                sheet.setColumnWidth((short) j, (short) 5000);
      //创建样式(使用工作本的对象创建)
      HSSFCellStyle cellStyle = workbook.createCellStyle();
      //创建字体的对象
      HSSFFont font = workbook.createFont();
      //将字体加粗
//      font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
      //字体大小
//      font.setFontHeightInPoints((short) 12);
      //将新设置的字体属性放置到样式中
      cellStyle.setFont(font);

//                cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平居中
      cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 下边框
      cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);// 左边框
      cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);// 上边框
      cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);// 右边框
//      cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
//      cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);

      if (fieldName.get(j) != null) {
        cell.setCellStyle(cellStyle);
        cell.setCellValue((String) fieldName.get(j));
      } else {
        cell.setCellStyle(cellStyle);
        cell.setCellValue("-");
      }
    }
  }

}
